package cz.muni.fi.pa165.sme.web.pages.user;

import java.text.DateFormat;
import java.text.NumberFormat;

import cz.muni.fi.pa165.sme.web.fo.UserFO;
import cz.muni.fi.pa165.sme.web.pages.TemplatePage;
import cz.muni.fi.pa165.sme.web.panels.sportActivityRecord.RecordListPanel;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeAction;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.ResourceModel;

/**
 * @author jirankova
 */
@AuthorizeAction(action = Action.RENDER, roles = {Roles.ADMIN, Roles.USER})
public class UserDetailPage extends TemplatePage {

	private final NumberFormat numberFormat;

	public UserDetailPage(final UserFO userFO, final String error) {
		//initialize number format
		numberFormat = NumberFormat.getNumberInstance(getLocale());

		//check if this page was called with an error message or not; if not error is not visible
		Label errorMessage;
		if (error == null || error.isEmpty()) {
			errorMessage = new Label("error", "");
			errorMessage.setVisible(false);
		} else {
			errorMessage = new Label("error", new ResourceModel(error));
			errorMessage.setVisible(true);
		}
		add(errorMessage);

		//page title
		add(new Label("title", new ResourceModel("user.detail.title")));

		//details
		add(new Label("firstNameLabel", new ResourceModel("firstNameLabel")));
		add(new Label("firstName", userFO.getFirstName()));

		add(new Label("lastNameLabel", new ResourceModel("lastNameLabel")));
		add(new Label("lastName", userFO.getLastName()));

		add(new Label("sexLabel", new ResourceModel("sexLabel")));
		add(new Label("sex", new ResourceModel(userFO.getSex().getName())));

		add(new Label("dateOfBirthLabel", new ResourceModel("dateOfBirth")));
		add(new Label("dateOfBirth", DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale()).format(
				userFO.getDateOfBirth())));

		add(new Label("heightLabel", new ResourceModel("heightLabel")));
		add(new Label("height", numberFormat.format(userFO.getHeight())));

		add(new Label("weightLabel", new ResourceModel("weightLabel")));
		add(new Label("weight", numberFormat.format(userFO.getWeight())));

		//link to edit user
		add(new Link<UserFO>("user.edit.link") {
			@Override
			public void onClick() {
				setResponsePage(new UserEditPage(userFO));
			}
		});

		//record list
		add(new Label("userRecords", new ResourceModel("user.records.title")));
		add(new RecordListPanel("userRecordList", userFO.getSportActivityRecords()));
		//
	}

	public UserDetailPage(final UserFO userFO) {
		this(userFO, null);
	}
}
